package com.lanou.sinasports.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.Gson;
import com.lanou.sinasports.R;
import com.lanou.sinasports.adapter.MatchHotAdapter;
import com.lanou.sinasports.beans.GsonMatchHot;
import com.lanou.sinasports.beans.MatchHot;
import com.lanou.sinasports.greenevents.MatHot;
import com.lanou.sinasports.greenevents.MatHotDao;
import com.lanou.sinasports.utils.DaoSingleton;
import com.lanou.sinasports.utils.GsonRequest;
import com.lanou.sinasports.utils.VolleySingleton;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/7.
 */
public class MatchHotFragment extends Fragment {
    private ListView listView;
    private MatchHotAdapter adapter;
    private Context context;
    private List<MatchHot> datas;
    private List<MatHot> data;
    private MatHotDao matHotDao;
    private String url=" http://client.mix.sina.com.cn/api/match_extra/get?app_key=2586208540&_sport_t_=livecast&__version__=3.0.1.05&__os__=android" +
            "&show_extra=1&f=livecast_id,LeagueType,status,Team1Id,Team2Id,Score1,Score2,MatchId,LiveMode,Discipline,data_from," +
            "Title,date,time,Team1,Team2,Flag1,Flag2,NewsUrl,VideoUrl,LiveUrl,LiveStatusExtra,VideoLiveUrl,VideoBeginTime,narrator," +
            "LeagueType_cn,Discipline_cn,comment_id,odds_id,VideoEndTime,if_rotate_video,LiveStatusExtra_cn,m3u8,android,period_cn," +
            "program,penalty1,penalty1,Round_cn,extrarec_ovxVideoId&_sport_a_=hotMatches";
    private VolleySingleton queue;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        this.context=context;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_matchhot,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        listView= (ListView) view.findViewById(R.id.lv_matchhot);


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        datas=new ArrayList<>();
        data=new ArrayList<>();
        queue= VolleySingleton.getInstance();

        matHotDao= DaoSingleton.getInstance().getMatHotDao();

        StringRequest request=new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson=new Gson();
                GsonMatchHot gsonMatchHot=gson.fromJson(response,GsonMatchHot.class);
                int size=gsonMatchHot.getResult().getData().getFull().size();
                for(int i=0;i<size;i++){
                    MatchHot matchHot=new MatchHot();
                    String date=gsonMatchHot.getResult().getData().getFull().get(i).getDate();
                    String leagueType=gsonMatchHot.getResult().getData().getFull().get(i).getLeagueType_cn();
                    String round=gsonMatchHot.getResult().getData().getFull().get(i).getRound_cn();
                    String team1=gsonMatchHot.getResult().getData().getFull().get(i).getTeam1();
                    String team2=gsonMatchHot.getResult().getData().getFull().get(i).getTeam2();
                    String score1=gsonMatchHot.getResult().getData().getFull().get(i).getScore1();
                    String score2=gsonMatchHot.getResult().getData().getFull().get(i).getScore2();
                    String flag1=gsonMatchHot.getResult().getData().getFull().get(i).getFlag1();
                    String flag2=gsonMatchHot.getResult().getData().getFull().get(i).getFlag2();


                    matchHot.setDate(date);
                    matchHot.setLeagueType_cn(leagueType);
                    matchHot.setRound_cn(round);
                    matchHot.setTeam1(team1);
                    matchHot.setTeam2(team2);
                    matchHot.setScore1(score1);
                    matchHot.setScore2(score2);
                    matchHot.setFlag1(flag1);
                    matchHot.setFlag2(flag2);

                    datas.add(matchHot);

                    MatHot matHot=new MatHot();
                    matHot.setDate(date);
                    matHot.setTitle(leagueType);
                    matHot.setRound(round);
                    matHot.setFlag1(flag1);
                    matHot.setTeam1(team1);
                    matHot.setScore1(score1);
                    matHot.setFlag2(flag2);
                    matHot.setTeam2(team2);
                    matHot.setScore2(score2);

                    data.add(matHot);
                }
                matHotDao.insertInTx(data);
                adapter=new MatchHotAdapter(context,datas);
                listView.setAdapter(adapter);

            }

        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

                Log.d("----------",getData()+"");
                adapter = new MatchHotAdapter(context, getData(), "22");
                listView.setAdapter(adapter);
            }
        });


        queue._addRequest(request);

    }

    public List<MatHot> getData(){
        if(matHotDao!=null){
            List<MatHot> list = matHotDao.queryBuilder().list();
            Log.i("test","------->>>"+list.size());
            return list;
        }else {
            return null;
        }
    }


}
